package xdu.lz.stage2_design_pattern.chapter13_producer_consume_pattern;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class ProducerThread extends Thread {

    private final MessageQueue messageQueue;

    private static final Random random = new Random(System.currentTimeMillis());

    private static final AtomicInteger counter = new AtomicInteger(0);

    public ProducerThread(String name,MessageQueue messageQueue) {
        super(name);
        this.messageQueue = messageQueue;
    }

    @Override
    public void run() {
        while (true){
            try {
                Message message = new Message("Integer-" + (counter.getAndIncrement()));
                messageQueue.put(message);
                System.out.println(Thread.currentThread()+getName()+" put "+message.getData());
                Thread.sleep(random.nextInt(10));
            } catch (InterruptedException e) {
                System.out.println("p被打断");
                break;
            }
        }
    }
}
